package timeitj.faces;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import timeitj.ejb.SessionFacade;
import timeitj.persistence.Person;

/**
 *
 * @author kunee
 */
public class UserController {
    
    @EJB
    private SessionFacade sessionFacade;
    
    private Person user;

    /**
     *  Returns the current User
     */
    public Person getUser() {
        
        if (user == null){
            user = sessionFacade.getCurrentUser();
        }
        
        return user;
    }
    
    public Map<String, Person> getAllUsers() {
        Map<String, Person> result = new HashMap<String, Person>();
        List<Person> list = sessionFacade.getAllUsers();
        for (Person p: list) {
            result.put(p.getName(), p);
        }
        return result;
    }
    
    /**
     * Ends the current session
     * @return Returns a String for page navigation
     */
    public String logOut(){
        
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
        session.invalidate();
        
        return "logout";
    }
}
